package zth;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;

public class listservlet extends HttpServlet
{
    private static final long serialVersionUID = 1108763402711986380L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.getRequestDispatcher("../mlist.jsp").forward(request, response);
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String sql = "SELECT * FROM information ORDER BY account DESC";


//        PrintWriter writer1 = response.getWriter();
//        writer1.println("hera668!");
//        writer1.flush();//ok！
//        List<Information> informations = null;
//        try {
//            informations = DBUtils.getInformation(sql);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        Writer writer = response.getWriter();
//                StringBuilder sb = new StringBuilder();
//                sb.append(PageUtils.getAdminHeader());
//        assert informations != null;
//        sb.append(toHtmlTable(informations));
//                sb.append(PageUtils.getEnd());
//                writer.write(sb.toString());
//                writer.flush();


        PrintWriter writer4 = response.getWriter();
        writer4.println("hera2!");
        writer4.flush();//ok！

    }
    private String toHtmlTable(List<Information> informations ) {
        StringBuilder sb = new StringBuilder();
        sb.append("<center><br><table style='width:70%' border='0' cellpadding='10'>");

        sb.append("<tr style='background-color:#336699;color:#ffffff'>" +
                "<th>name</th><th>sex</th><th>tel</th><th>qq</th>" +
                "<th></th><th></th></tr>");
        for (int i=0; i<informations.size(); i++) {
            Information information = informations.get(i);
            if (i % 2 == 0) {
                sb.append("<tr style='background-color:#cdcdcd'>");
            } else {
                sb.append("<tr style='background-color:#efefef'>");
            }
            sb.append("<td>").append(information.getaccount()).append("</td>");
            sb.append("<td><b>").append(information.getpassword()).append("</b></td>");
            sb.append("<td>").append(information.getsex()).append("</td>");
            sb.append("<td>").append(information.gettype()).append("</td>");
           // sb.append("<td><img width='100px' src='/javaweb-2021-master/upload/").append(book.getPics().replaceAll(",", "")).append("'></img></td>");
//            sb.append("<td>").append(
//                    String.format("<a href='./blogphotoinf?id=%s'>修改</a>", photoinf.getId())
//            ).append("</td>");详情从这个地方跳转到那个人的页面上去
            sb.append("<td>").append(
                    String.format("<a href='./editBook?id=%s'>修改</a>", information.getaccount())
            ).append("</td>");//修改和删除的操作还没完成
            sb.append("<td>").append(
                    String.format("<a href='./deleteBook?id=%s'>删除</a>", information.getaccount())
            ).append("</td>");
            sb.append("</tr>");
        }
        sb.append("</table></center>");
        return sb.toString();
    }



}
